package com.xq.jltx.frescosimple.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.xq.jltx.frescosimple.R;
import com.xq.jltx.frescosimple.adapters.FrescoGridViewAdapter;
import com.xq.jltx.frescosimple.adapters.ImageListAdapter;
import com.xq.jltx.frescosimple.config.Constants;
import com.xq.jltx.frescosimple.config.imagepipeline.ConfigConstants;
import com.xq.jltx.frescosimple.config.imagepipeline.ImagePipelineConfigFactory;
import com.xq.jltx.frescosimple.display.PerfListener;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016年02月03日.
 */

public class ImageGridViewFragment extends BackHandledFragment{
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private GridView gridView;

    private int mCurrentAdapterIndex;
    private ImageListAdapter mCurrentAdapter;
    private String[] imgList;

    private PerfListener mPerfListener;

    public ImageGridViewFragment() {
    }

    public static ImageGridViewFragment newInstance(String[] params,int currentIndex){
        ImageGridViewFragment imageGridViewFragment = new ImageGridViewFragment();
        Bundle args = new Bundle();
        args.putCharSequenceArray(ARG_PARAM1,params);
        args.putInt(ARG_PARAM2,currentIndex);
        imageGridViewFragment.setArguments(args);
        return imageGridViewFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if(getArguments() != null){
            imgList = (String[])getArguments().getCharSequenceArray(ARG_PARAM1);
            mCurrentAdapterIndex = getArguments().getInt(ARG_PARAM2);
        }
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gridview,null,false);
        if(view instanceof GridView){
            gridView = (GridView)view;
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        setAdapter(mCurrentAdapterIndex);
        super.onViewCreated(view, savedInstanceState);
    }
    /**
     *
     * @param index
     */
    private void setAdapter(int index){
        if (mCurrentAdapter != null) {
            mCurrentAdapter.shutDown();
            mCurrentAdapter = null;
            System.gc();
        }

        mCurrentAdapterIndex = index;
        mPerfListener = new PerfListener();
        switch (index) {
            case Constants.FRESCO_INDEX:
                mCurrentAdapter = new FrescoGridViewAdapter(getContext(),imgList,  ConfigConstants.getImagePipelineConfig(getContext()),mPerfListener);
                break;
            case Constants.FRESCO_OKHTTP_INDEX:
                mCurrentAdapter = new FrescoGridViewAdapter(getContext(),imgList,  ImagePipelineConfigFactory.getOkHttpImagePipelineConfig(getContext()),mPerfListener);
                break;
            default:
                mCurrentAdapter = null;
                break;
        }

        gridView.setAdapter(mCurrentAdapter);
        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                return false;
            }
        });
        if(mCurrentAdapter !=null){
            mCurrentAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public boolean onBackPressed() {
        return false;
    }

    @Override
    public void onUpdateFragment(Bundle bundle) {
        if(bundle != null){
            int position = bundle.getInt(Constants.LOADER_INDEX);
            setAdapter(position);
        }
        super.onUpdateFragment(bundle);
    }


}
